Return to the menu   Select another DES Module

Demonstrates the features of the ErrorMessage properties

Validators use an Error Message to communicate the problem found by the condition to the user. There are actually two error messages that you can define per Validator.

  • The ErrorMessage property's text appears directly on the Validator control through its ErrorFormatter property.
  • The SummaryErrorMessage property's text appears in the ValidationSummary control. This allows you to provide a different level of details on each message, perhaps keeping the message next to the field shorter.

Tokens in Error Messages

Error messages support tokens – terms embedded into brackets – which get replaced by properties or runtime values determined when the condition is evaluated. This allows you to build standard messages where the programmer selects the right message for the situation, complete with tokens, and it applies the properties and runtime values automatically. By having runtime values, you can provide the user with specifics about what is wrong with their entry, such as the count of the number of characters they typed and how much that exceeds your limits.

Here are some common tokens:

  • {LABEL}, {LABEL2}, {LABEL3}, and {LABEL4} - Uses the field's label defined in the Label, SecondLabel, Label2, Label3, or Label4 property.
  • {NEWLINE} - Defines a linebreak that works in different UIs. When on the browser window, it is replaced by the <br /> tag. When in an alert, it is replaced by a linefeed character.
  • {TEXTVALUE} and {TEXTVALUE2} - The current text value of the data entry control specified by ControlIDToEvaluate and SecondControlIDToEvaluate.
  • {MINIMUM} and {MAXIMUM} - Shows the values of the Minimum and Maximum properties on these validators: RangeValidator, TextLengthValidator, WordCountValidator, CountSelectionsValidator, and CountTrueConditionsValidator.
  • {VALUETOCOMPARE} - Use the value of the ValueToCompare property on the CompareToValueValidator.
  • {COUNT} and {COUNT:singular:plural} - Used to show the count and a descriptive label for when the count is 1 or something else. For example: "{COUNT} {COUNT:character:characters}". Used on these validators: TextLengthValidator, WordCountValidator, CountSelectionsValidator, and CountTrueConditionsValidator.
  • {EXCEEDS} and {EXCEEDS:singular:plural} - Used to show how much the count exceeds the limit and a descriptive label for when the exceeds value is 1 or something else. Used on these validators: TextLengthValidator, WordCountValidator
  • There are several more tokens specific to validators. {DIFFERENCEVALUE} and {DIFFRESULT} on the DifferenceValidator; {UNWANTED} on the UnwantedWordsValidator.

Tokens are automatically assigned to a style sheet class to make their text stand out. Their styles are defined in the [web app]\DES\Appearance\Validation\Validators.css file.


Controls

Your Age:  *
Details:
 


Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> <asp:Label ID="YourAgeLabel" runat="server">Your Age:</asp:Label> <des:TextBox ID="Age" runat="server" Width="40px" /> <des:RequiredTextValidator ID="rtvAge" runat="server" ControlIDToEvaluate="Age" ErrorMessage="Required" SummaryErrorMessage="{LABEL} is required" Label-LabelControlID="YourAgeLabel" Label-TrimTrailingSymbol="true" Label-Case="TitleCase" ShowRequiredFieldMarker="true" > <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:RequiredTextValidator> <des:DataTypeCheckValidator ID="DataTypeCheckValidator1" runat="server" ControlIDToEvaluate="Age" ErrorMessage="Enter a number" SummaryErrorMessage="Enter a number in {LABEL}. You entered [{TEXTVALUE}]." Label-LabelControlID="YourAgeLabel" Label-TrimTrailingSymbol="true" Label-Case="TitleCase" DataType="Integer" > <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:DataTypeCheckValidator> <des:RangeValidator ID="RangeValidator1" runat="server" ControlIDToEvaluate="Age" ErrorMessage="Between {MINIMUM} and {MAXIMUM}" SummaryErrorMessage="{LABEL} must be between {MINIMUM} and {MAXIMUM}" Label-LabelControlID="YourAgeLabel" Label-TrimTrailingSymbol="true" Label-Case="TitleCase" DataType="Integer" Minimum="1" Maximum="120"> <ErrorFormatterContainer> <des:TextErrorFormatter Display="Dynamic" /> </ErrorFormatterContainer> </des:RangeValidator> <br/> Details: <des:TextBox ID="Details" runat="server" Width="300px" /> <des:TextLengthValidator ID="tlvDetails" runat="server" ControlIDToEvaluate="Details" ErrorMessage="Exceeds {MAXIMUM} by {EXCEEDS} {EXCEEDS:character:characters}" SummaryErrorMessage="{LABEL} exceeds {MAXIMUM} by {EXCEEDS} {EXCEEDS:character:characters}" Label-Text="Details" Maximum="20"/> <hr/> <des:Button ID="Submit" runat="server" Text="Submit" OnClick="Button1_Click" />  <br/><br/> <des:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Fix these errors" />

Return to the menu   Select another DES Module